// pages/Maintenance/Maintenance.js
const httpRequest = require('../../utils/http_utils.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: "维护保养",
    currentTab: 0,
    showIs: false,
    menulist: [],
    PageSize: 10, //每页条数
    CurrentPage: 1, //当前页数
    TaskType: 1, //当天维保任务
    maintainList: [], //当日维保任务集合
    noMore: false, //刷新提示语
    downloading: false,
    Refresh:false, //是否刷新
    navData: [{
      text: '责任班组'
    },
    {
      text: '外委单位'
    }
    ],
    IsDelegate:2, //外委单位
    DelegateList:[], //外委单位集合
    scrollHeight: 0, //滚动区域高度
    TaskState:null, //工单状态
    customHeight: 0, //自定义高度
  },

  //切换
  switchNav: function(event) {
    var cur = event.currentTarget.dataset.currenttab;
    console.log(cur + "-----------点击切换")
    if (this.data.currentTab == cur) {
      return 
    } else {
      if (cur == 0){
        this.setData({
          currentTab: cur,
          maintainList: [], //当日维保任务集合
        })
        this.initData();
      }else{
        this.setData({
          currentTab: cur,
          PageSize: 10, //单页数据条数
          CurrentPage: 1, //当前页码
          DelegateList: [], //当日维保任务集合
        })
        this.initList();
      }
    }
  },

  //菜单跳转
  tiaozhuan: function(e) {
    var rowguid = e.currentTarget.dataset.rowguid; //获取菜单rowguid
    var url = e.currentTarget.dataset.url; //获取按钮跳转地址
    console.log(url + "-------------扫一扫")
    if (url.indexOf('saoyisao') == -1) {
      wx.navigateTo({
        url: url + '?ParentGuid=' + rowguid,
      })
    } else {
      this.scancode();
    }
  },

  //设备跳转
  Device: function(e) {
    wx.navigateTo({
      url: '../Device/Device?ClassifyGuid=' + e.currentTarget.dataset.classifyguid + '&ClassifyName=' + e.currentTarget.dataset.classifyname
    })
  },

  //跳转分享
  isDelegate: function(e){
    console.log(e.currentTarget.dataset.taskstate+"------------------------工单状态")
    if (e.currentTarget.dataset.taskstate == 0 || e.currentTarget.dataset.taskstate == 4 ){
      let TaskGuid = e.currentTarget.dataset.taskguid;
      let EquipmentName = e.currentTarget.dataset.equipmentname;
      let EquipmentLocation = e.currentTarget.dataset.equipmentlocation;
      let TaskBeginTime = e.currentTarget.dataset.taskbegintime;
      let TaskEndTime = e.currentTarget.dataset.taskendtime;
      let TaskState = e.currentTarget.dataset.taskstate;

      wx.navigateTo({
        url: '../IsDelegate/IsDelegate?TaskGuid=' + TaskGuid + "&EquipmentName=" + EquipmentName + "&EquipmentLocation=" + EquipmentLocation + "&TaskBeginTime=" + TaskBeginTime + "&TaskEndTime=" + TaskEndTime + "&TaskState=" + TaskState 
      })
    } else if (e.currentTarget.dataset.taskstate == 3){
      console.log(JSON.stringify(e.currentTarget.dataset) + "------------传参")

      let TaskGuid = e.currentTarget.dataset.taskguid;
      let EquipmentName = e.currentTarget.dataset.equipmentname;
      let RecordBeginTime = e.currentTarget.dataset.recordbegintime;
      let TaskCompleteTime = e.currentTarget.dataset.taskcompletetime;
      let EquipmentLocation = e.currentTarget.dataset.equipmentlocation;
      let TaskBeginTime = e.currentTarget.dataset.taskbegintime;
      let TaskEndTime = e.currentTarget.dataset.taskendtime;
      let TaskState = e.currentTarget.dataset.taskstate;

      wx.navigateTo({
        url: '../MaintainHistoryDetails/MaintainHistoryDetails?TaskGuid=' + TaskGuid + "&EquipmentName=" + EquipmentName + "&RecordBeginTime=" + RecordBeginTime + "&TaskCompleteTime=" + TaskCompleteTime + "&EquipmentLocation=" + EquipmentLocation + "&TaskBeginTime=" + TaskBeginTime + "&TaskEndTime=" + TaskEndTime + "&TaskState=" + TaskState
      })
    } 
    
  },

  //获取当前维保任务
  initData: function() {
    var that = this;
    var param = {};
    httpRequest.PostValidateRequest(httpRequest.apiInfo.GetEquipmentTypeApi, param,
      function(data) {
        console.log("-------------当天维保任务------------" + JSON.stringify(data))
        var listArray = that.data.maintainList;
        if (data.pageData.length == 0 || data.pageData == null || data.pageData == undefined) {
          that.setData({
            showIs: true
          })
        } else {
          for (var i = 0; i < data.pageData.length; i++) {
            listArray.push(data.pageData[i])
          }
          that.setData({
            maintainList: listArray,
            totalNum: data.totalNum,
            noMore: false,
            showIs: false
          })
        }
      },
      function(err) {})
  },

  //获取外委单位
  initList: function(){
    var that = this;
    var param = {
      PageSize: that.data.PageSize,
      CurrentPage: that.data.CurrentPage,
      TaskType: that.data.TaskType,
      IsDelegate: that.data.IsDelegate,
    };
    console.log(JSON.stringify(param))
    httpRequest.PostValidateRequest(httpRequest.apiInfo.GetMaintenanceTaskHisMobileApi, param,
      function (data) {
        console.log("-------------当天维保任务------------" + JSON.stringify(data))
        var listArray = that.data.DelegateList;
        if (data.Data.length == 0 || data.Data == null || data.Data == undefined) {
          that.setData({
            showIs: true
          })
        } else {
          for (var i = 0; i < data.Data.length; i++) {
            listArray.push(data.Data[i])
            that.setData({
              TaskState: data.Data[i].TaskState
            })
          }
          that.setData({
            DelegateList: listArray,
            totalNum: data.TotalNum,
            noMore: false,
            showIs: false
          })
        }
      },
      function (err) { })
  },

  //上拉加载
  scrollToLower: function () {
    var that = this
    console.log(that.data.totalNum + "_______总条数")
    if (that.data.currentTab == 1){
      var pages = Math.round((that.data.totalNum / 10) * 100) / 100
      if (that.data.CurrentPage > pages) {
        that.setData({
          noMore: true
        })
        setTimeout(() => {
          that.setData({
            noMore: false
          })
        }, 2000)
      } else {
        that.setData({
          noMore: false,
          downloading: true
        })
        setTimeout(() => {
          that.setData({
            downloading: false
          })
        }, 3000)
        that.setData({
          CurrentPage: that.data.CurrentPage + 1
        })
        that.initList();
      }
    }
    
  },

  scancode: function() {
    var that = this
    wx.scanCode({
      success: function(res) {
        wx.navigateTo({
          url: '../ScanTask/ScanTask?code=' + res.result,
        })
      },
      fail: function(res) {
        console.log(JSON.stringify(res))
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    this.setData({
      customHeight: ((75 + app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 90
    })

    console.log(options.ParentGuid + '---------------------------维保菜单')
    if (options.ParentGuid != undefined && options.ParentGuid != '') {
      var menu_list = this.data.menulist;
      var storage_list = wx.getStorageSync('UserInfo').AppLogin.AppModuleDTO.filter(function(item) {
        return item.ParentGuid === options.ParentGuid;
      });

      console.log(storage_list + '---------------------------维保菜单')

      for (var i = 0; i < storage_list.length; i++) {
        var param = {
          RowGuid: storage_list[i].RowGuid,
          Name: storage_list[i].ModuleName,
          Icon: storage_list[i].BigIconAddress,
          ModuleUrl: storage_list[i].ModuleUrl
        }
        menu_list.push(param)
      }
      //根据RowGuid去重
      const RowGuid = 'RowGuid';
      menu_list = menu_list.reduce((all, next) => all.some((atom) => atom[RowGuid] == next[RowGuid]) ? all : [...all, next], []);
      this.setData({
        menulist: menu_list
      })
      console.log('menulist---', this.data.menulist)
    }
    this.setData({
      scrollHeight: app.globalData.titleBarHeight + app.globalData.statusBarHeight + 46
    })
    this.refreshView = this.selectComponent("#refreshView");
    // this.initData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.currentTab == 0) {
      this.setData({
        maintainList: []
      })
      this.initData();
    }
    if (this.data.currentTab == 1){
      this.setData({
        PageSize: 10, //单页数据条数
        CurrentPage: 1, //当前页码
        DelegateList: [], //当日维保任务集合
      })
      this.initList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  //触摸开始
  handletouchstart: function(event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function(event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function(event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function(event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function(event) {
    this.refreshView.onPageScroll(event)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this
    if (that.data.currentTab == 0){
      setTimeout(() => {
        console.log("scrollToUpper")
        that.setData({
          maintainList: []
        })
        that.initData();
        that.refreshView.stopPullRefresh()
      }, 1000)
    }else{
      setTimeout(() => {
        console.log("scrollToUpper")
        that.setData({
          PageSize: 10, //单页数据条数
          CurrentPage: 1, //当前页码
          DelegateList: [], //当日维保任务集合
        })
        that.initList();
        that.refreshView.stopPullRefresh()
      }, 1000)
    }
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})